package se.lth.student.axisandroidcam.client;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;

import se.lth.cs.fakecamera.Axis211A;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class CameraAggregator extends BaseAdapter implements Observer{
	private Context mContext;
	private LinkedList<Bitmap> thumbnails;
	private LinkedList<Long> timestamps;
	private LinkedList<Camera> cameras;
    private static final int TARGET_HEIGHT = 150;
    private static final int TARGET_WIDTH = 120;
    //private static final int BUFFER_OFFSET = 4;
    
    
    public CameraAggregator(Context c){
    	super();
    	thumbnails = new LinkedList<Bitmap>();
    	timestamps = new LinkedList<Long>();
    	mContext = c;
    }


	public int getCount() {
		return thumbnails.size();
	}

	public Object getItem(int nbr) {
		return cameras.get(nbr);
	}
	
	

	public long getItemId(int position) {
        return position;
	}

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) { // if it's not recycled, initialize some
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(TARGET_WIDTH,
                                TARGET_HEIGHT));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                //imageView.setPadding(8, 8, 8, 8);
        } else {
                imageView = (ImageView) convertView;
        }

        imageView.setImageBitmap(thumbnails.get(position));

        return imageView;

}

	
	public synchronized boolean addImage(byte[] img, int dataLength, int cameraNbr){
        Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        

        
        BitmapFactory.decodeByteArray(img, 0, dataLength);
        if (options.outHeight != -1 && options.outWidth != -1) {
                Toolbox.printDebug("Image is valid");
                
                // Only scale if we need to
                Boolean scaleByHeight = !(Math.abs(options.outHeight
                                - TARGET_HEIGHT) >= Math.abs(options.outWidth
                                - TARGET_WIDTH));

                // Load, scaling to smallest power of 2 that'll get it <=
                // desired
                // dimensions
                double sampleSize = scaleByHeight ? options.outHeight
                                / TARGET_HEIGHT : options.outWidth / TARGET_WIDTH;
                options.inSampleSize = (int) Math.pow(2d, Math.floor(Math
                                .log(sampleSize)
                                / Math.log(2d)));

                // Do the actual decoding
                options.inJustDecodeBounds = false;
                Bitmap image = BitmapFactory.decodeByteArray(img, 0, dataLength, options);

                
                thumbnails.add(image);
                CameraAggregator.this.notifyDataSetChanged();
                Toolbox.printDebug("Image is added");
                return true;
        } else {
               Toolbox.printDebug("     Image is invalid");
               return false;
        }
		
		
	
	}


	public void update(Observable observable, Object arg) {
		DataWrapper wrap = (DataWrapper) arg;
		addImage(wrap.data, wrap.data.length, wrap.cameraNbr);
	}
}
